import tkinter as tk
import requests
import os
from tkinter import messagebox

header={'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'}
root="./image/"


win=tk.Tk()
win.title("ssss")
win.resizable(width=False,height=False)
win.geometry("400x400+600+200")

url=tk.StringVar()
entry1=tk.Entry(textvariable=url)
entry1.pack()



def fun1():
    S=['path', 'url_info', 'file_geshi']
    for SRT in S:
        SRT=tk.StringVar()
    path = os.getcwd()
    url_info = entry1.get()
    file_path = path + url_info.aplit("/")[-1]
    file_name = url_info.aplit("/")[-1]
    file_geshi = url_info.aplit(".")[-1]
    try:
        r=requests.get(url_info, headers=header)
        r.raise_for_status()
        r.encoding=r.apparent_encoding
        nm=messagebox.askyesno(title="恭喜",message="爬取成功是否保存？")
        if nm:
            if not os.path.exists(path):
                os.mkdir(path)
            if not os.path.exists(file_path):
                with open(file_path, "wb") as f:
                    f.write(r.content)
                    f.close()
                    messagebox.showinfo(title="恭喜", message="文件保存成功")
            else:
                messagebox.showerror(title="错误提示",message="文件已经存在")

    except:
        messagebox.showerror(title="温馨提示",message="爬取失败")


tb1=tk.Button(text="点我",command=fun1)
tb1.pack()



win.mainloop()